import { myLogin, getUerBaseInfo, getEmployeeBaseInfo } from '@/api/user'
import { getToken, removeToken, setToken } from '@/utils/auth'
// import { getUerBaseInfoAPI, getEmployeeBaseInfoAPI } from '../../api/index.js'

const state = {
  token: getToken(),
  userInfo: {}
}

const mutations = {
  // mutations 用来修改state
  // 定义修改state的方法
  setToken(state, token) {
    state.token = token
    // 调用方法，做token数据持久化
    setToken(token)
  },
  removeToken(state) {
    state.token = undefined
    removeToken()
  },
  setUserInfo(state, userInfo) {
    state.userInfo = userInfo
  },
  removeUserInfo(state) {
    state.userInfo = {}
  }
}

const actions = {
  // actions 要修改state，那必须通过mutations
  // 定义发请求的action方法
  async login(context, loginForm) {
    const token = await myLogin(loginForm)
    context.commit('setToken', token)
  },
  // 退出操作清除token
  logout(context) {
    context.commit('removeToken')
    context.commit('removeUserInfo')
  },
  async getInfo(context) {
    const m = await getUerBaseInfo()
    const n = await getEmployeeBaseInfo(m.userId)
    context.commit('setUserInfo', { ...m, ...n })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

